﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVDL4.VO;
using System.Data;
using System.Data.SqlClient;


namespace TVDL4.DAO
{
    class VatLieuDAO
    {
        Random rnd = new Random();
        private dbConnection conn;
        public VatLieuDAO()
        {
            conn = new dbConnection();
        }
        /// <summary>
        /// tra ve mot thong tin vat lieu theo ten vat lieu
        /// </summary>
        /// <param name="tenVatLieu"></param>
        /// <returns></returns>
        public DataTable SearchByName(string tenVatLieu)
        {
            string query = string.Format("select * from DMVATLIEU where Tenvatlieu like @vatlieu ");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@vatlieu", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(tenVatLieu);
            return conn.executeSelectQuery(query, sqlParameters);
        }
        /// <summary>
        /// lay tat ca cac du lieu tu table DMVATLIEU
        /// </summary>
        /// <returns></returns>
        public DataTable SearchAll()
        {
            string query = string.Format("select TENVATLIEU,KEYSL from DMVATLIEU");
            SqlParameter[] sqlParameters = new SqlParameter[0];
            return conn.executeSelectQuery(query, sqlParameters);
        }
        /// <summary>
        /// insert table DMVATLIEU
        /// </summary>
        /// <param name="tenVatLieu"></param>
        /// <returns></returns>
        public VatLieuVO InsertRecord(VatLieuVO vatlieuVO)
        {
            string query = string.Format("Insert into DMVATLIEU(KEYSL,TENVATLIEU) VALUES (@keysl,@vatlieu)");
            SqlParameter[] sqlParameters = new SqlParameter[2];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(Module.CreateRunninNo(rnd));
            sqlParameters[1] = new SqlParameter("@vatlieu", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(vatlieuVO.TenVatLieu);
            if (conn.executeInsertQuery(query, sqlParameters))
            {
                vatlieuVO.KeySL = sqlParameters[0].Value.ToString();
                return vatlieuVO;
            }
            else
            {
                return null;
            }
           
        }
        /// <summary>
        /// Update Vật Liệu
        /// </summary>
        /// <param name="vatlieuVO"></param>
        /// <returns></returns>
        public bool UpdateRecord(VatLieuVO vatlieuVO)
        {
            string query = string.Format("UPDATE DMVATLIEU SET TENVATLIEU = @vatlieu WHERE KEYSL = @keysl");
            SqlParameter[] sqlParameters = new SqlParameter[2];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(vatlieuVO.KeySL);
            sqlParameters[1] = new SqlParameter("@vatlieu", SqlDbType.NVarChar);
            sqlParameters[1].Value = Convert.ToString(vatlieuVO.TenVatLieu);
            return conn.executeInsertQuery(query, sqlParameters);
           
        }
        /// <summary>
        /// Xóa vat liej
        /// </summary>
        /// <param name="keySL"></param>
        /// <returns></returns>
        public bool DeleteRecord(string keySL)
        {
            string query = string.Format("Delete DMVATLIEU Where KEYSL = @keysl");
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@keysl", SqlDbType.NVarChar);
            sqlParameters[0].Value = Convert.ToString(keySL);
            return conn.executeInsertQuery(query, sqlParameters);

        }
       
    }
}
